<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row>
				<div class="flex justify-between w-full mb8">
					<el-space>
						<el-input placeholder="请输入单位名称" v-model="state.queryForm.deptName" />
						<el-button type="primary" @click="getDataList(true)">搜索</el-button>
						<el-button icon="refresh" @click="resetQuery()"></el-button>
					</el-space>
					<div>
						<el-button type="success" icon="plus" class="mr-2" @click="formDialogRef.openDialog()" v-auth="'platform_basicExpensesDeptDetail_add'">
							新增配置</el-button
						>
						<!-- <el-button type="warning" icon="Checked" class="mr-2" @click="handlePey">代付配置</el-button> -->
					</div>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@current-change="handleCurrentChange"
				highlight-current-row
				row-key="deptId"
				:expand-row-keys="expandRowKeys"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="单位名称" show-overflow-tooltip min-width="200" />
				<!-- <el-table-column prop="deliveryMethod" label="是否代付" width="100">
					<template #default="{ row }">
						<div>
							<el-button link v-if="row.deliveryMethod === 20" type="primary" size="small" @click="handlePaySetDetail(row)">代付</el-button>
							<span v-if="row.deliveryMethod === 10" class="text-info" size="small">未开启</span>
						</div>
					</template>
				</el-table-column> -->
				<el-table-column prop="platformFeeYears" label="平台年限" show-overflow-tooltip width="90">
					<template #default="{ row }">
						<div>
							{{ row.platformFeeYears === 0 ? '-' : row.platformFeeYears }}
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="platformExpireDate" label="平台到期时间" show-overflow-tooltip min-width="180">
					<template #default="{ row }">
						<div>
							<span class="text-error" v-if="isOverDate(row.platformExpireDate)">已到期</span>
							<span v-else>{{ row.platformExpireDate }}</span>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="ttsRemainNum" label="语音剩余（分钟）" show-overflow-tooltip width="160">
					<template #default="{ row }">
						<span>{{ row.ttsRemainNum === 999999 ? '无限制' : row.ttsRemainNum }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="smsRemainNum" label="短信剩余（条）" show-overflow-tooltip width="160">
					<template #default="{ row }">
						<span>{{ row.smsRemainNum === 999999 ? '无限制' : row.smsRemainNum }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="deviceRemainNum" label="设备剩余（台）" show-overflow-tooltip width="160" />
				<!-- <el-table-column prop="smsExpirationDate" label="短信代付到期时间" show-overflow-tooltip min-width="180">
					<template #default="{ row }">
						<div>
							<span class="text-error" v-if="isOverDate(row.smsExpirationDate)">已到期</span>
							<span v-else>{{ row.smsExpirationDate }}</span>
						</div>
					</template>
				</el-table-column> -->
				<!-- <el-table-column prop="ttsExpirationDate" label="语音代付到期时间" show-overflow-tooltip min-width="180">
					<template #default="{ row }">
						<div>
							<span class="text-error" v-if="isOverDate(row.ttsExpirationDate)">已到期</span>
							<span v-else>{{ row.ttsExpirationDate }}</span>
						</div>
					</template>
				</el-table-column> -->
				<el-table-column label="操作" width="80" fixed="right">
					<template #default="scope">
						<el-button text type="primary" @click="handleDetail(scope.row.deptId)">详情</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" /> -->
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
		<!-- 代付配置 -->
		<pay-set-dialog ref="paySetDialogRef" @refresh="getDataList(false)" />
		<!-- 代付配置详情 -->
		<pay-set-detail-dialog ref="paySetDetailDialogRef" />
	</div>
</template>

<script setup lang="ts" name="systemBasicExpensesDeptDetail">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList } from '/@/api/tariff/basicExpensesDeptDetail';
import { useMessage } from '/@/hooks/message';
import { useRouter } from 'vue-router';
const router = useRouter();
// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const PaySetDialog = defineAsyncComponent(() => import('./paySet.vue'));
const PaySetDetailDialog = defineAsyncComponent(() => import('./paySetDetail.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
const paySetDialogRef = ref();
const paySetDetailDialogRef = ref();
// 搜索变量
const queryRef = ref();
const currentRow = ref({ deptId: null });
// 多选变量
const selectObjs = ref([]) as any;
const handleCurrentChange = (val: any) => {
	currentRow.value = val;
};
const expandRowKeys = computed(() => {
	return state.dataList?.map((d) => d.deptId);
});
const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		deptName: '',
	},
	isPage: false,
	pageList: fetchList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	state.queryForm.deptName = '';
	getDataList();
};

// 代付配置
const handlePey = () => {
	if (!currentRow.value?.deptId) {
		useMessage().wraning('请点击列表选择一条您要操作的数据！');
		return;
	}
	unref(paySetDialogRef).openDialog(currentRow.value);
};
// 代付配置
const handlePaySetDetail = (row: any) => {
	paySetDetailDialogRef.value.openDialog(row);
};
// 判断到期
const isOverDate = (date: string) => {
	if (!date) return false;
	if (new Date(date).getTime() > new Date().getTime()) return false;
	return true;
};
const handleDetail = (deptId: string) => {
	router.push({
		path: '/tariff/basicExpensesDeptDetail/detail',
		query: {
			deptId: deptId,
		},
	});
};
</script>
